package com.suxin.algorithm.table;

import io.shardingsphere.api.algorithm.sharding.PreciseShardingValue;
import io.shardingsphere.api.algorithm.sharding.standard.PreciseShardingAlgorithm;
import lombok.extern.slf4j.Slf4j;

import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;

/**
 * @author Tang
 * @classname GoodsTablePreciseShardingAlgorithm
 * @description [  ]
 * @date 2021/3/9 10:32
 */
@Slf4j
public class GoodsTableCreateTimePreciseShardingAlgorithm implements PreciseShardingAlgorithm<Date> {

    public static final String DATE_FORMAT_YYYY = "yyyy";

    @Override
    public String doSharding(Collection<String> collection, PreciseShardingValue<Date> preciseShardingValue) {
        String logicTableName = preciseShardingValue.getLogicTableName() + "_";
        SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT_YYYY);
        String dateStr = dateFormat.format(preciseShardingValue.getValue());
        for (String tableName : collection) {
            if (tableName.equals(logicTableName + dateStr)) {
                return tableName;
            }
        }
        throw new IllegalArgumentException();
    }
}